package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.CustomTemplateFieldAddDTO;
import org.jsola.hr.dto.CustomTemplateFieldBatchDTO;
import org.jsola.hr.dto.CustomTemplateFieldUpdateDTO;
import org.jsola.hr.query.CustomTemplateFieldQuery;
import org.jsola.hr.service.ICustomTemplateFieldService;
import org.jsola.hr.vo.CustomFieldVO;
import org.jsola.hr.vo.CustomTemplateFieldListVO;
import org.jsola.hr.vo.CustomTemplateFieldVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 自定义模板字段
 *
 * @author zhr
 */
@RestController("hrCustomTemplateFieldController")
@RequestMapping(HrConstants.API_PATH + "/v1/customTemplateFields")
public class CustomTemplateFieldController implements ICustomTemplateFieldControllerAPI {

    @Autowired
    private ICustomTemplateFieldService customTemplateFieldService;

    @Override
    @GetMapping("/selectAllEmpFiled/{companyId}")
    public Result<List<String>> selectAllEmpFiled(@PathVariable String companyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增自定义模板字段权限校验

        return Result.success(
                customTemplateFieldService.selectAllEmpFiled(companyId, tokenUser)
        );
    }

    @Override
    @PostMapping
    public Result<CustomTemplateFieldVO> save(@Valid @RequestBody CustomTemplateFieldAddDTO customTemplateFieldAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增自定义模板字段权限校验

        return Result.success(
                customTemplateFieldService.save(customTemplateFieldAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody CustomTemplateFieldUpdateDTO customTemplateFieldUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改自定义模板字段权限校验

        int count = customTemplateFieldService.update(customTemplateFieldUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody CustomTemplateFieldBatchDTO customTemplateFieldBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<CustomTemplateFieldAddDTO> customTemplateFieldAddDTOList = customTemplateFieldBatchDTO.getCustomTemplateFieldAddDTOList();
        if (!CollectionUtils.isEmpty(customTemplateFieldAddDTOList)) {
            customTemplateFieldService.batchSave(customTemplateFieldAddDTOList, tokenUser);
        }
        // 修改
        List<CustomTemplateFieldUpdateDTO> customTemplateFieldUpdateDTOList = customTemplateFieldBatchDTO.getCustomTemplateFieldUpdateDTOList();
        if (!CollectionUtils.isEmpty(customTemplateFieldUpdateDTOList)) {
            customTemplateFieldUpdateDTOList.parallelStream().forEach(
                    customTemplateFieldUpdateDTO -> {
                        customTemplateFieldService.update(customTemplateFieldUpdateDTO, tokenUser);
                    }
            );
        }
        // 删除
        List<String> deleteCustomTemplateFieldIdList = customTemplateFieldBatchDTO.getDeleteCustomTemplateFieldIdList();
        if (!CollectionUtils.isEmpty(deleteCustomTemplateFieldIdList)) {
            customTemplateFieldService.deleteByIds(tokenUser, deleteCustomTemplateFieldIdList.toArray(new String[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{customTemplateFieldId}")
    public Result<Integer> delete(@PathVariable String customTemplateFieldId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除自定义模板字段权限校验

        int count = customTemplateFieldService.deleteByIds(tokenUser, customTemplateFieldId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] customTemplateFieldIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除自定义模板字段权限校验

        int count = customTemplateFieldService.deleteByIds(tokenUser, customTemplateFieldIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{customTemplateFieldId}")
    public Result<CustomTemplateFieldVO> findById(@PathVariable String customTemplateFieldId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看自定义模板字段权限校验

        return Result.success(
                customTemplateFieldService.selectById(customTemplateFieldId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<CustomTemplateFieldListVO>> listAll(@Valid CustomTemplateFieldQuery customTemplateFieldQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看自定义模板字段权限校验

        return Result.success(
                customTemplateFieldService.select(customTemplateFieldQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/selectAllField")
    public Result<CustomFieldVO> selectAllField(CustomTemplateFieldQuery customTemplateFieldQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看自定义模板字段权限校验

        return Result.success(
                customTemplateFieldService.selectAllField(customTemplateFieldQuery, tokenUser)
        );
    }

    @Override
    @GetMapping
    public Result<Page<CustomTemplateFieldListVO>> page(@Valid CustomTemplateFieldQuery customTemplateFieldQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                customTemplateFieldService.selectPage(customTemplateFieldQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/listDefaultField")
    public Result<CustomFieldVO> listDefaultField(CustomTemplateFieldQuery customTemplateFieldQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                customTemplateFieldService.listDefaultAndAdditionalField(customTemplateFieldQuery, tokenUser)
        );
    }

    @GetMapping("/createDefaultField")
    public Result<List<CustomTemplateFieldVO>> createDefaultField() {

        return Result.success(
                customTemplateFieldService.createDefaultField()
        );
    }


}

